package com.xy.core.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class LocalDateTime {
	private static final Logger log = LoggerFactory.getLogger(LocalDateTime.class);
	public static final String yyyy="yyyy";
	public static final String yyyyMM="yyyy-MM";
	public static final String yyyyMMdd="yyyy-MM-dd";
	public static final String yyyyMMddHH="yyyy-MM-dd HH";
	public static final String yyyyMMddHHmm="yyyy-MM-dd HH:mm";
	public static final String yyyyMMddHHmmss="yyyy-MM-dd HH:mm:ss";
	public static final String HHmm = "HH:mm";
	public static final String HHmmss = "HH:mm:ss";

	public static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


	public static String now(){
		return sdf.format(new Date());
	}
	public static String now(String format){
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(new Date());
	}
	public static String getDate(Date date,String format){
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(date);
	}
	public static String getDateStr(Date date){
		return sdf.format(date);
	}
	public static Date getDate(String dateStr,String format) throws ParseException{
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.parse(dateStr);
	}
	public static Date getDate(String dateStr) throws ParseException{
		return sdf.parse(dateStr);
	}
	/**
	 * 日期提取（日期转换）
	 * @param dateStr 2017-02-10 22:10:00
	 * @param prefixFormat yyyy-MM-dd HH:mm:ss
	 * @param suffixFormat yyyy-MM-dd
	 * @return 2017-02-10
	 * @throws ParseException
	 */
	public static String getDate(String dateStr,String prefixFormat,String suffixFormat) throws ParseException{
		SimpleDateFormat sdf = new SimpleDateFormat(prefixFormat);
		Date date=sdf.parse(dateStr);
		sdf = new SimpleDateFormat(suffixFormat);
		return sdf.format(date);
	}
	public static Date parse(String str) throws ParseException{
		Map<String, String> object = new HashMap<String, String>();
		String xxString =null;
		boolean b=false;
		SimpleDateFormat sdf = null;
		
		object.put("yyyy","^[0-9]{4}$");
		object.put("yyyy-MM","^[0-9]{4}-[0-9]{1,2}$");
		object.put("yyyy-MM-dd","^[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}$");
		object.put("yyyy-MM-dd HH","^[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}\\s[0-9]{1,2}$");
		object.put("yyyy-MM-dd HH:mm","^[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}\\s[0-9]{1,2}:[0-9]{1,2}$");
		object.put("yyyy-MM-dd HH:mm:ss","^[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}\\s[0-9]{1,2}:[0-9]{2}:[0-9]{1,2}$");
		for(Entry<String, String> entry:object.entrySet()){
			
			xxString=matcher(str, entry.getValue());
			b=xxString!=null;
//			System.out.println("匹配格式："+entry.getValue()+":"+b);
			if(b){
				sdf = new SimpleDateFormat(entry.getKey());
				Date date=sdf.parse(str);
//				System.out.println(sdf.format(date));
				return date;
			}
		}
		object.put("long","^[0-9]+$");
		xxString=matcher(str, object.get("long"));
		if(xxString!=null){
			Long long1=new Long(xxString);
			Date date = new Date(long1);
//			System.out.println(date.toLocaleString());
			return date;
		}
		return null;
	}
	public static String matcher(String str,String reg){
		Pattern pattern = Pattern.compile(reg);
		Matcher matcher = pattern.matcher(str);
		while(matcher.find()){
			return matcher.group();
		}
		return null;
		
	}
	/**
	 * 得到时间戳
	 * @param creattime
	 * @return
	 */
	public static Long getDateTime(Date creattime) {
		// TODO Auto-generated method stub
		return creattime.getTime();

	}
}
